package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;
    //修改用户信息
    //Request URL: http://localhost/api/user/wxLogin/updateUser
    @TsLogin
    @Operation(summary = "修改用户信息")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo){
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层方法
        userInfoService.updateUser(userId,userInfoVo);
        //返回数据
        return Result.ok();

    }

    //获取登录用户信息
    //Request URL: http://localhost/api/user/wxLogin/getUserInfo
    @TsLogin//没有这个注解不会把用户id存储到线程中
    @Operation(summary = "获取用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用服务层的方法
        UserInfoVo userInfoVo=userInfoService.getUserInfo(userId);
        //返回数据
        return Result.ok(userInfoVo);
    }

    //微信登录
    //Request URL: http://localhost/api/user/wxLogin/wxLogin/0a3qvn2w3K3yA532je2w3fPM1o0qvn2C
    @Operation(summary = "微信登录")
    @GetMapping("/wxLogin/{code}")
    public Result wxLogin(@PathVariable String code){
        log.info("登录开始");
        //获取登录成功后的code
        Map<String,Object> map=userInfoService.wxLogin(code);
        //返回数据
        return Result.ok(map);
    }
}
